package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCartMapper;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.vo.WxCouponMyListVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MYM
 * @Date: 2024/05/20/20:53
 * @Description:
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    MarketCartMapper marketCartMapper;

    @Override
    public List<MarketCouponUser> myListPage(MarketUser user, Short status, Integer page, Integer limit) {
        MarketCouponUserExample example = new MarketCouponUserExample();
        Integer id = user.getId();
        example.createCriteria().andUserIdEqualTo(id);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(example);
        return marketCouponUsers;
    }

    public List<WxCouponMyListVo> myList(MarketUser user, Short status, Integer page, Integer limit) {
        List<MarketCouponUser> marketCouponUsers = myListPage(user, status, page, limit);
        List<WxCouponMyListVo> wxCouponMyListVos = new ArrayList<>();
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            Integer id = marketCouponUser.getId();
            Integer cid = marketCouponUser.getCouponId();
            MarketCouponExample example = new MarketCouponExample();
            example.createCriteria().andIdEqualTo(cid).andStatusEqualTo(status);
            List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
            if (marketCoupons.size() > 0) {
                MarketCoupon marketCoupon = marketCoupons.get(0);
                wxCouponMyListVos.add(new WxCouponMyListVo(false, marketCoupon.getId()
                        , marketCoupon.getDesc(), marketCoupon.getDiscount(), marketCoupon.getEndTime(), id,
                        marketCoupon.getMin(), marketCoupon.getName(), marketCoupon.getStartTime(), marketCoupon.getTag()));
            }
        }
        return wxCouponMyListVos;
    }

    @Override
    public List<MarketCoupon> list(Integer page, Integer limit) {
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria().andTypeEqualTo((short) 0);
        PageHelper.startPage(page, limit);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        for (MarketCoupon marketCoupon : marketCoupons) {
            marketCoupon.setTotal(null);
            marketCoupon.setGoodsValue(null);
            marketCoupon.setGoodsType(null);
            marketCoupon.setLimit(null);
            marketCoupon.setType(null);
            marketCoupon.setStatus(null);
            marketCoupon.setCode(null);
            marketCoupon.setTimeType(null);
            marketCoupon.setStartTime(null);
            marketCoupon.setEndTime(null);
            marketCoupon.setAddTime(null);
            marketCoupon.setUpdateTime(null);
            marketCoupon.setDeleted(null);
        }
        return marketCoupons;
    }

    @Override
    public List<MarketCoupon> selectList(Integer cartId, Integer grouponRuleId) {
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andIdEqualTo(cartId);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(example);
        Integer userId = marketCarts.get(0).getUserId();
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        couponUserExample.createCriteria().andUserIdEqualTo(userId);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(couponUserExample);
        List<MarketCoupon> marketCoupons = new ArrayList<>();
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            Integer currencyCid = marketCouponUser.getCouponId();
            MarketCouponExample couponExample = new MarketCouponExample();
            couponExample.createCriteria().andIdEqualTo(currencyCid).andStatusEqualTo((short) 0);
            List<MarketCoupon> unSelectMarketCoupons = marketCouponMapper.selectByExample(couponExample);
            marketCoupons.add(unSelectMarketCoupons.get(0));
        }
        return marketCoupons;
    }

    @Override
    public void receive(Integer couponId, MarketUser user) {
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria().andIdEqualTo(couponId).andStatusEqualTo((short) 0);
        MarketCoupon marketCoupon = marketCouponMapper.selectByExample(example).get(0);
        Integer userId = user.getId();
        LocalDateTime time = LocalDateTime.now();
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(userId);
        marketCouponUser.setCouponId(marketCoupon.getId());
        marketCouponUser.setStatus(marketCoupon.getStatus());
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        marketCouponUser.setEndTime(marketCoupon.getEndTime());
        marketCouponUser.setAddTime(time);
        marketCouponUser.setDeleted(marketCoupon.getDeleted());
        marketCouponUserMapper.insertSelective(marketCouponUser);
    }

    @Override
    public Object exchange(String code, MarketUser user) {
        Object result = "兑换码不正确";
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria().andCodeEqualTo(code);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        if (marketCoupons.size() != 0) {
            MarketCoupon marketCoupon = marketCoupons.get(0);
            Integer userId = user.getId();
            LocalDateTime time = LocalDateTime.now();
            MarketCouponUser marketCouponUser = new MarketCouponUser();
            marketCouponUser.setUserId(userId);
            marketCouponUser.setCouponId(marketCoupon.getId());
            marketCouponUser.setStatus(marketCoupon.getStatus());
            marketCouponUser.setStartTime(marketCoupon.getStartTime());
            marketCouponUser.setEndTime(marketCoupon.getEndTime());
            marketCouponUser.setAddTime(time);
            marketCouponUser.setDeleted(marketCoupon.getDeleted());
            marketCouponUserMapper.insertSelective(marketCouponUser);
            result = null;
        }
        return result;
    }
}
